﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLPM
{
    public partial class frmQuanLyDonVi : DevComponents.DotNetBar.Office2007Form
    {
        public frmQuanLyDonVi()
        {
            InitializeComponent();
        }

        public int TachKhoaChinh(string KhoaChinh, char TachLayKiTu)
        {
            string[] temp;
            temp = KhoaChinh.Split(new char[] { TachLayKiTu });
            return Convert.ToInt32(temp[1]);
        }


        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadDonVi()
        {
            DonViBUS dvBus = new DonViBUS();
            DataTable dt = new DataTable();
            dt = dvBus.LayBangDonVi();
            dt.Columns.Add("STT");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int j = i + 1;
                dt.Rows[i]["STT"] = j.ToString();
            }
            dtgvDanhSachDonVi.DataSource = dt;
            dtgvDanhSachDonVi.Columns["STT"].Width = 40;
            dtgvDanhSachDonVi.Columns["STT"].DisplayIndex = 0;
            dtgvDanhSachDonVi.Columns["STT"].ReadOnly = true;
        }

        private void frmQuanLyDonVi_Load(object sender, EventArgs e)
        {
            DonViBUS dvBus = new DonViBUS();
            DataTable dtdv = new DataTable();
            dtdv = dvBus.LayBangDonVi();
            //Đưa danh sách don vi lên DatagridView
            dtgvDanhSachDonVi.DataSource = dtdv;

            if (dtdv.Rows.Count == 0)
            {
                LoadDonVi();
                txtMaDonVi.Text = "DV01";
            }
            else
            {
                DataTable dt = dvBus.LayMaDonVi();
                int maDV = TachKhoaChinh(dt.Rows[0][0].ToString(), 'V') + 1;
                txtMaDonVi.Text = string.Format("DV{0:00}", maDV);
                LoadDonVi();
            }
        }

        private void btnThemDonVi_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thêm đơn vị??", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                try
                {
                    DonViDTO dvDto = new DonViDTO();
                    DonViBUS dvBus = new DonViBUS();
                    dvDto.MaDV = txtMaDonVi.Text;
                    dvDto.TenDV = this.txtTenDonVi.Text;

                    dvBus.ThemDV(dvDto);
                    DataTable dt = dvBus.LayMaDonVi();
                    int madv = TachKhoaChinh(dt.Rows[0][0].ToString(), 'V') + 1;
                    txtMaDonVi.Text = string.Format("DV{0:00}", madv);
                    LoadDonVi();
                }
                catch
                {
                    MessageBox.Show("Insert Error! Check your information again!!!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }

        }

        private void btnXoaDonVi_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa đơn vị??", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                DonViBUS dvBus = new DonViBUS();
                dvBus.XoaDV(txtMaDonVi.Text);
                LoadDonVi();
            }

        }

        private void dtgvDanhSachDonVi_Click(object sender, EventArgs e)
        {
            DataGridViewRow dtgvR = dtgvDanhSachDonVi.CurrentRow;
            txtMaDonVi.Text = dtgvR.Cells["MaDV"].Value.ToString();
            txtTenDonVi.Text = dtgvR.Cells["TenDV"].Value.ToString();
            if (dtgvR.Cells["MaDV"].Value.ToString() == "" || dtgvR.Cells["TenDV"].Value.ToString() == "")
            {
                DonViBUS dvBus = new DonViBUS();
                DataTable dt = dvBus.LayMaDonVi();
                int maDV = TachKhoaChinh(dt.Rows[0][0].ToString(), 'V') + 1;
                txtMaDonVi.Text = string.Format("DV{0:00}", maDV);
            }

        }

        private void btnCapNhatDonVi_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn cập nhật đơn vị??", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                DonViDTO dvDto = new DonViDTO();
                DonViBUS dvBus = new DonViBUS();
                dvDto.MaDV = txtMaDonVi.Text;
                dvDto.TenDV = this.txtTenDonVi.Text;

                dvBus.SuaDV(dvDto);
                LoadDonVi();
            }
        }      
    }
}